<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>memory leaks</title>
  </head>
  <body>
    <script>
      // recursion
      function recursion() {
        // console.count()
        // let a= new Array(1000000).fill(0);
        // recursion();
      }
      recursion();

      function fn1() {
        var obj = { name: "yu", age: 10 };
        // 调用完就被释放
      }
      function fn2() {
        var obj = { name: "yu", age: 11 };
        return obj;
      }
      var a = fn1();
      var b = fn2();

      // const object1 = {a:new Array(1999),b:new Array(2999)} // b 无法被回收，这个是不对的
      // setInterval(()=>{
      //   console.log(object1.a)
      // },1000)

      // closure
      // let store
      // function outer(){
      //   let largeD = new Array(10000000)
      //   let preStore= store
      //   function inner(){
      //     if(preStore) {return largeD}
      //   }
      //   return function(){}
      // }

      // setInterval(function(){
      //   store = outer()
      // },1000)

      // 脱离的 DOM 元素
      function createDetachedElement() {
        const div = document.createElement("div");
        div.id = "detached";
        return div;
      }
      const detachedDiv = createDetachedElement();
      document.body.appendChild(detachedDiv);
      function deleteDetachedElement() {
        document.body.removeChild(document.getElementById("detached"));
      }
      deleteDetachedElement();
    </script>
  </body>
</html>
